'use strict';

const conn = require('../../libs/Conn');
const util = require('../../libs/Utils');
const moment = require('moment');

module.exports = async function (params) {
    let typeList;
    let progress_type;
    if(params.progress_type === '4' || params.progress_type === '5' || params.progress_type === '6'){
        typeList = [4,5,6] ;
        progress_type = 'important_part_acceptance';
    }else if(params.progress_type === '11' || params.progress_type === '12' || params.progress_type === '13'|| params.progress_type === '14'|| params.progress_type === '15'|| params.progress_type === '16'|| params.progress_type === '17'|| params.progress_type === '18'){
        typeList = [11,12,13,14,15,16,17,18] ;
        progress_type = 'quality_sampling';
    }else{
        switch (params.progress_type) {
            case '1':
                typeList = [1];
                progress_type = 'foundation_test_tanks';
                break;
            case '2':
                typeList = [2];
                progress_type = 'material_check_a';
                break;
            case '3':
                typeList = [3];
                progress_type = 'foundation_test';
                break;
            case '7':
                typeList = [7];
                progress_type = 'first_conversion_reinforcement';
                break;
            case '8':
                typeList = [8];
                progress_type = 'material_check_b';
                break;
            case '9':
                typeList = [9];
                progress_type = 'standard_reinforcement';
                break;
            case '10':
                typeList = [10];
                progress_type = 'top_reinforcement';
                break;
            case '19':
                typeList = [19];
                progress_type = 'concrete_structure';
                break;
            case '20':
                typeList = [20];
                progress_type = 'material_check_c';
                break;
            case '21':
                typeList = [21];
                progress_type = 'main_structure';
                break;
            case '22':
                typeList = [22];
                progress_type = 'other_projects';
                break;
        }
    }
    let progress_no;
    let nowYear = moment().format('YYYY');
    let dataBaseYearSql = `SELECT progress_no FROM engineering_progress_information_data WHERE progress_type = '${params.progress_type}' ORDER BY progress_no DESC LIMIT 1;SELECT COUNT(information_id) AS count FROM engineering_progress_information_data WHERE progress_type in (${typeList}) AND engineering_id = '${params.engineering_id}';SELECT * FROM engineering_progress_file WHERE engineering_id = '${params.engineering_id}'`;
    let dataBaseYear = await conn(dataBaseYearSql);
    let year;
    if(dataBaseYear[0].length>0){
        year = dataBaseYear[0][0].progress_no.substring(0,4);
        if(year===nowYear){
            progress_no =  parseInt(dataBaseYear[0][0].progress_no)+1
        }else{
            if(params.progress_type>=10){
                progress_no =  nowYear+'00'+params.progress_type+'00001'
            }else{
                progress_no =  nowYear+'000'+params.progress_type+'00001'
            }
        }
    }else{
        if(params.progress_type>=10){
            progress_no =  nowYear+'00'+params.progress_type+'00001'
        }else{
            progress_no =  nowYear+'000'+params.progress_type+'00001'
        }
    }
    let statusSql = ``;
    let progress_status;
    if(dataBaseYear[2][0]){
        progress_status  = dataBaseYear[2][0][progress_type];
    }
    if(dataBaseYear[1][0].count ===0){
        statusSql = `UPDATE engineering_progress_file SET ${progress_type} = 2 WHERE engineering_id = '${params.engineering_id}';`;
        if(progress_status<2){
            progress_status = 2;
        }
    }
    let information_id = util.uuid();
    if(params.supervision_date===''){
        params.supervision_date = null
    }
    if(params.inspection_date === ''){
        params.inspection_date = null
    }
    if(params.rectification_date === ''){
        params.rectification_date = null
    }
    let obj = {
        information_id:information_id,
        engineering_id:params.engineering_id,
        progress_type:params.progress_type,
        progress_no:progress_no,
        supervision_date:params.supervision_date,
        reporter_name:params.reporter_name,
        inspection_date:params.inspection_date,
        reporter_phone:params.inspection_no,
        inspection_name:params.inspection_name,
        inspection_result:params.inspection_result,
        rectification_notice:params.rectification_notice,
        rectification_reply:params.rectification_reply,
        rectification_date:params.rectification_date,
        supervision_details:params.supervision_details,
        remarks:params.remarks,
        progress_status:progress_status,
    };
    let insertUserSql = ``;
    if(params.user_ids){
        let user_ids=params.user_ids;
        user_ids=user_ids.split(',');
        for (let user_id in user_ids){
            insertUserSql += `INSERT INTO progress_information_user_middle (information_id, user_id) VALUES ('${information_id}', '${user_ids[user_id]}');`;
        }
    }
    let sql = `INSERT INTO engineering_progress_information_data SET ?;`+statusSql+insertUserSql;

    await conn(sql,[obj]);
    return {success: true};

};